﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Brain
{

    public class BCFFile
    {
        public BCFFileData[] Files { get; set; }

        public void Write(Stream stream)
        {
            using (BinaryWriter writer = new BinaryWriter(stream))
            {
                writer.Write("BCFFile");
                writer.Write(0.1f);

                writer.Write(Files.Length);
                for (int i = 0; i < Files.Length; i++)
                {
                    BCFFileData data = Files[i];
                    writer.Write(data.Data.Length);
                    writer.Write(data.Data);
                }
            }
        }

        /// <summary>
        /// 
        /// Exceptions:
        /// <para>  System.IO.DirectoryNotFoundException </para>
        /// </summary>
        /// <param name="DirectoryPath"></param>
        /// <param name="SavePath"></param>
        /// <returns></returns>
        public static BCFFile Compress(string DirectoryPath, Stream SaveStream)
        {
            DirectoryInfo dir = new DirectoryInfo(DirectoryPath);
            if (!dir.Exists)
            {
                throw new DirectoryNotFoundException("Directory " + DirectoryPath + " not found by System!");
            }

            FileInfo[] files = dir.GetFiles();
            BCFFileData[] fileData = new BCFFileData[files.Length];

            for (int i = 0; i < files.Length; i++)
            {
                FileInfo file = files[i];

                BCFFileData data = new BCFFileData(file);
                //data.Compress();

                fileData[i] = data;
            }

            BCFFile compressedFile = new BCFFile();
            compressedFile.Files = fileData;

            compressedFile.Write(SaveStream);

            return compressedFile;
        }
    }
}
